<template lang="html">
    <div>
      <van-nav-bar 
        :title="pageTitle"
        class="nav-bar-custom"
        left-arrow
        @click-left="goBack"
      >
      </van-nav-bar>

      <div class="button-group">
        <button 
          @click="showPublishedEvents" 
          :class="{ 'active-button': currentView === 'published' }">
          创建的活动
        </button>
        <button 
          @click="showJoinedEvents" 
          :class="{ 'active-button': currentView === 'joined' }">
          参加的活动
        </button>
      </div>

      <div class="filter-buttons" v-if="currentView === 'published'">
        <button 
          v-for="filter in filters" 
          :key="filter" 
          :class="{ active: currentFilter === filter }" 
          @click="currentFilter = filter">
          {{ filter }}
        </button>
      </div>

      <div class="filter-buttons" v-if="currentView === 'joined'">
        <button 
          v-for="filter in filters_" 
          :key="filter" 
          :class="{ active: currentFilter_ === filter }" 
          @click="currentFilter_ = filter">
          {{ filter }}
        </button>
      </div>



      <div class="content">
          <!-- 创建的活动列表 -->
          <div v-if="currentView === 'published'" class="events-scroll-container">
            <div class="published-events">
              <div class="event" v-for="(event, index) in filteredEvents" :key="index" @click="goToEventDetail(event)">
                <div class="event-title">{{ event.title }}</div>
                <span :class="['event-status', statusMapping[event.status].class]">
                  {{ statusMapping[event.status].text }}
                </span>
                <button class="delete-event-button" @click="deleteEvent(event, index)">×</button>
              </div>
            </div>
          </div>

          <!-- 参加的活动列表 -->
          <div v-if="currentView === 'joined'" class="events-scroll-container">
            <div class="joined-events">
              <div class="event" v-for="(event, index) in filteredEvents_" :key="index" @click="goToEventDetail(event)">
                <div class="event-title">{{ event.title }}</div>
                <span :class="['event-status', statusMapping[event.status].class]">
                  {{ statusMapping[event.status].text }}
                </span>
                <button class="delete-event-button" @click="deleteEvent(event, index)">×</button>
              </div>
            </div>
          </div>


        <div v-if="currentView === 'published'" class="create-event-button-container">
          <button @click="createEvent" class="create-event-button">创建活动</button>
        </div>
      </div>

    </div>
  </template>
  
  <script>
// import axios from 'axios';
// import authApi from "@/utils/api/authApi";
import userApi from "@/utils/api/userApi";
import {showToast} from "vant";


  export default {
    data() {
      return {
        pageTitle: '个人活动中心',
        result: '',
        keywords: '',
        isLoading: false,
        currentView: '',
        publishedEvents: [
          { title: '活动1', status: 'pubed', detail: '这是活动1的详情...' },
          { title: '活动2', status: 'unpubed', detail: '这是活动2的详情...' },
          { title: '活动3', status: 'fined', detail: '这是活动1的详情...' },
          { title: '活动4', status: 'unfined', detail: '这是活动2的详情...' },
          { title: '活动5', status: 'pubed', detail: '这是活动1的详情...' },
          { title: '活动6', status: 'pubed', detail: '这是活动2的详情...' },
          { title: '活动7', status: 'pubed', detail: '这是活动1的详情...' },
          { title: '活动8', status: 'pubed', detail: '这是活动2的详情...' },
        ],
        joinedEvents: [
          { title: '活动a', status: 'end', detail: '这是活动1的详情...' },
          { title: '活动b', status: 'no_end', detail: '这是活动2的详情...' },
          { title: '活动c', status: 'end', detail: '这是活动1的详情...' },
          { title: '活动d', status: 'end', detail: '这是活动2的详情...' },
          { title: '活动e', status: 'no_end', detail: '这是活动1的详情...' },
          { title: '活动f', status: 'end', detail: '这是活动2的详情...' },
          { title: '活动g', status: 'no_end', detail: '这是活动1的详情...' },
          { title: '活动h', status: 'no_end', detail: '这是活动2的详情...' },
          { title: '活动i', status: 'end', detail: '这是活动1的详情...' },
          { title: '活动j', status: 'end', detail: '这是活动2的详情...' },
        ],
        statusMapping: {
          pubed: { text: '已发表', class: 'published' },
          unpubed: { text: '未发表', class: 'unpublished' },
          fined: { text: '已结束', class: 'finished' },
          unfined: { text: '未结束', class: 'unfinished' },
          end: { text: '已结束', class: 'end' },
          no_end: { text: '未结束', class: 'no_end' }
      },

        currentFilter: '全部', // 默认显示所有活动
        filters: ['全部', '已发表', '未发表', '已结束', '未结束'],

        currentFilter_: '全部', // 默认显示所有活动
        filters_: ['全部', '已结束', '未结束'],
        

      }
    },

    created() {
      this.currentView = 'published';
      // this.getUserId();
      this.listAct('publish', '', true, {page: 0,size: 20});
    },
    computed: {
          filteredEvents() {
            if (this.currentFilter === '全部') {
              return this.publishedEvents;
            }
            return this.publishedEvents.filter(event => {
              switch (this.currentFilter) {
                case '已发表':
                  return event.status === 'pubed';
                case '未发表':
                  return event.status === 'unpubed';
                case '已结束':
                  return event.status === 'fined';
                case '未结束':
                  return event.status === 'unfined';
                default:
                  return true;
              }
            });
          },
          filteredEvents_() {
            if (this.currentFilter_ === '全部') {
              return this.joinedEvents;
            }
            return this.joinedEvents.filter(event => {
              switch (this.currentFilter_) {
                case '已结束':
                  return event.status === 'end';
                case '未结束':
                  return event.status === 'no_end';
                default:
                  return true;
              }
            });
          }
        },

    methods: {
    //   toSearch(keywords) {
    //     if(keywords !== '') {
    //       this.isLoading = true
    //       this.result = ''
    //       if(keywords.trim()) {
    //         this.$http.get('/api/search/' + keywords)
    //           .then(res => {
    //             this.result = res.data.result.list
    //             this.isLoading = false
    //         })
    //       }
    //     }
    //   },
      goBack() {
        this.$router.back(-1)
      },
    //   toSearchDetail(item) {
    //     this.$router.push({name: 'searchDetail', params: { item : item }})
    //   },

      showPublishedEvents() {
        this.currentView = 'published';
        console.log("published events.")
        this.listAct('publish', '', true, {page: 0,size: 20});
      },
      showJoinedEvents() {
        this.currentView = 'joined';
        console.log("joined events.")
        this.listAct('apply', '', true, {page: 0,size: 20});
      },
      goToEventDetail(event) {
        console.log("Navigating to event detail page for:", event.title);
        this.$router.push('/act/info')

      },
      createEvent() {
        console.log("Creating new event.");
        this.$router.push('/act/edit')

      },
      deleteEvent(event, index) {
        if (confirm(`确定要删除活动 "${event.title}" 吗？`)) {
          // 删除逻辑
          if (this.currentView === 'published') {
            this.publishedEvents.splice(index, 1);
          } else {
            this.joinedEvents.splice(index, 1);
          }
        }
      },

      // async loginTest() {
      //   //TODO 测试登录
      //   let userId = await authApi.loginTest(this.userId);
      //   showToast(`登陆成功，用户ID：${userId}`)
      //   this.cookie = document.cookie
      // },

      async getUserId(){
        let userId = await userApi.getUserId();
        console.log(userId);

      },

      async listAct(type, sort, desc, page){
        let res = await userApi.listAct(type, sort, desc, page);
        console.log(res);
      }

   
    }
  }
  </script>
  
  <style lang="css" scoped>
.nav-bar-custom {
  color: white;
}
.button-group {
  display: flex;
  justify-content: stretch; /* 填满整个水平空间 */
  padding-top: 0.5rem;
}

.button-group button {
  flex-grow: 1;
  padding: 20px;
  border: none;
  border-radius: 30px; /* 圆润的边角 */
  font-weight: bold;
  font-size: 1.2rem;
  transition: all 0.3s ease;
  outline: none;
  margin: 0 10px; /* 两边的间隙 */
  cursor: pointer;
}

/* 默认样式 */
.button-group button {
  background-color: #f5f5f5; /* 淡色背景 */
  color: #333; /* 深色文字 */
}

/* 激活状态的按钮样式 */
.active-button {
  background: linear-gradient(135deg, #f6d365 0%, #fda085 100%); /* 渐变色背景 */
  color: white;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* 阴影效果 */
}

/* 鼠标悬停在按钮上的样式 */
.button-group button:hover:not(.active-button) {
  background-color: #e2e2e2; /* 鼠标悬停时的背景色 */
}


.event {
  background: #ffffff; /* 白色背景 */
  border-radius: 8px; /* 圆角 */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 轻微的阴影效果 */
  padding: 20px;
  margin-bottom: 20px; /* 事件之间的间隙 */
  transition: transform 0.3s ease, box-shadow 0.3s ease; /* 平滑的过渡效果 */
  cursor: pointer;
  border: 1px solid #e1e1e1; /* 添加一个浅色边框 */
  position: relative; /* 为伪元素定位做准备 */
}

/* 添加一个伪元素来创建双层边框效果 */
.event::before {
  content: '';
  position: absolute;
  top: -5px;
  right: -5px;
  bottom: -5px;
  left: -5px;
  border-radius: 12px; /* 确保伪元素边框圆角比容器的大 */
  border: 1px solid #d7d7d7; /* 第二层边框的颜色稍深 */
  z-index: -1; /* 确保伪元素在容器之下 */
}

/* 如果想要边框有点颜色渐变效果，可以这样 */
.event::after {
  content: '';
  position: absolute;
  top: -1px;
  right: -1px;
  bottom: -1px;
  left: -1px;
  border-radius: inherit; /* 继承父元素的圆角 */
  z-index: -2;
  background: linear-gradient(45deg, rgba(255,255,255,0), rgba(0,0,0,0.1));
  pointer-events: none; /* 确保不会干扰事件的点击 */
}


.event-title {
  font-size: 18px; /* 字体大小 */
  color: #333; /* 深灰色字体 */
  font-weight: 600; /* 字体加粗 */
}

/* 鼠标悬停在事件上时的效果 */
.event:hover {
  transform: translateY(-5px); /* 轻微上移效果 */
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15); /* 加深阴影 */
}

.create-event-button {
  background-color: #4CAF50; /* 绿色背景 */
  color: white; /* 白色文字 */
  border: none;
  padding: 15px 30px; /* 内部填充 */
  font-size: 1rem; /* 字体大小 */
  font-weight: bold; /* 字体加粗 */
  border-radius: 50px; /* 圆角边框 */
  cursor: pointer; /* 鼠标悬停时显示手型 */
  transition: background-color 0.3s ease; /* 平滑过渡效果 */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); /* 轻微的阴影 */
  position: absolute; /* 绝对定位 */
  bottom: 20px; /* 距离底部的距离 */
  left: 50%; /* 水平居中 */
  transform: translateX(-50%); /* 确保完全居中 */
}

.create-event-button:hover {
  background-color: #45a049; /* 鼠标悬停时的背景色变深 */
}

.delete-event-button {
  position: absolute;
  top: 10px;
  right: 10px;
  border: none;
  background-color: transparent;
  color: #ff4d4f;
  cursor: pointer;
  font-size: 24px; /* Adjust size as needed */
  line-height: 1;
  padding: 0;
}

.delete-event-button:hover {
  color: #ff7875;
}

.events-scroll-container {
  max-height: calc(100vh - 180px); /* 调整这个值以适应固定部分的总高度 */
  overflow-y: auto; /* 允许在必要时垂直滚动 */
  overflow-x: hidden; /* 隐藏水平滚动条 */
}

.event-status {
  display: inline-block;
  padding: 3px 6px;
  font-size: 0.75rem;
  border-radius: 12px;
  color: white;
  position: absolute;
  top: 10px;
  right: 35px; /* Adjust this value to not overlap with delete button */
}

.published {
  background-color: #4CAF50;
}

.unpublished {
  background-color: #f44336;
}

.finished {
  background-color: #9E9E9E;
}

.unfinished {
  background-color: #2196F3;
}

.end {
  background-color: #9E9E9E;
}
.no_end {
  background-color: #4CAF50;
}

.delete-event-button {
  /* ...existing styles... */
  z-index: 10; /* Make sure delete button is above status tags */
}

.filter-buttons {
  display: flex;
  justify-content: space-around; /* Distribute space evenly around items */
  align-items: center; /* Align items vertically in the center */
  padding: 10px;
  margin-top: 10px; /* Spacing from the top element */
}

.filter-buttons button {
  flex: 1; /* Each button will take up equal space */
  padding: 10px 0; /* Padding top and bottom */
  border: 1px solid #cccccc; /* Light grey border */
  background-color: #f8f8f8; /* Light grey background */
  color: #333; /* Dark grey text */
  text-align: center;
  border-radius: 5px; /* Slightly rounded corners for a modern look */
  cursor: pointer;
  transition: all 0.3s ease; /* Smooth transition for hover effects */
  margin: 0 5px; /* Spacing between buttons */
  font-size: 0.9rem; /* Font size */
  font-weight: 600; /* Bold font weight */
}

.filter-buttons button:hover {
  border-color: #007bff; /* Blue border on hover */
  background-color: #e7f4ff; /* Very light blue background on hover */
}

.filter-buttons button.active {
  border-color: #007bff; /* Blue border for active state */
  background-color: #007bff; /* Blue background for active state */
  color: white; /* White text for active state */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle shadow for depth */
}



  </style>
  